﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ParameterManagementSystem
{
    public partial class TagPanel : UserControl
    {
        public TagPanel()
        {
            InitializeComponent();
            tagList = new Dictionary<int, Tag>();
            relationsList = new List<XmlTagRelation>();
            selectedXmlID = new List<int>();
        }

        public void ClearTagsList()
        {
            tagList.Clear();
            listBoxTags.Items.Clear();
        }

        public void TagList_AddItem(Tag item)
        {
            tagList.Add(item.id, item);
            listBoxTags.Items.Add(item.name);
        }

        public void LoadTagsFromDatabase(DataBase dataBase)
        {
            ClearTagsList();
            Tag[] tagsArray = dataBase.LoadTagsArray();
            foreach (Tag item in tagsArray)
            {
                if (!tagList.ContainsKey(item.id))
                {
                    TagList_AddItem(item);
                }
            }
            ((Form1)(this.ParentForm)).UpdateComboBoxTags();
        }

        public void LoadRelationsFromDatabase(DataBase dataBase)
        {
            relationsList.Clear();
            relationsList = dataBase.LoadRelationArray();
        }

        public void SaveRelations(DataBase dataBase)
        {
            XmlTagRelation[] relationsArray = new XmlTagRelation[relationsList.Count];
            relationsList.CopyTo(relationsArray, 0);
            dataBase.SaveRelations(relationsArray);
        }

        public void SaveTags(DataBase dataBase)
        {
            Tag[] tagsArray = new Tag[tagList.Values.Count];
            tagList.Values.CopyTo(tagsArray, 0);
            dataBase.SaveTags(tagsArray);
        }

        public List<int> SelectedXmlID
        {
            get
            {
                return selectedXmlID;
            }

            set
            {
                selectedXmlID = value;

                listBoxXmlTags.Items.Clear();

                if (selectedXmlID.Count == 0)
                    return;
                
                List<int> tag_list = new List<int>();

                for (int i = 0; i < selectedXmlID.Count; i++)
                    foreach (XmlTagRelation relation in relationsList)
                    {
                        if (relation.xmlFileID == selectedXmlID[i])
                        {
                            bool found = false;
                            //listBoxXmlTags.Items.Add(tagList[relation.tagID].name);
                            foreach(int id in tag_list)
                                if (id == relation.tagID)
                                {
                                    found = true;
                                    break;
                                }
                            if(!found)
                                tag_list.Add(relation.tagID);
                        }
                    }

                foreach (int id in tag_list)
                    listBoxXmlTags.Items.Add(tagList[id].name);
            }
        }

        public Dictionary<int, Tag> tagList;

        public List<XmlTagRelation> relationsList;

        private void buttonAddTag_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxTag.Text))
            {
                return;
            }
            if (listBoxTags.Items.Contains(textBoxTag.Text))
            {
                return;
            }
            listBoxTags.Items.Add(textBoxTag.Text);
            Tag tag = new Tag();

            Tag[] tagsArray = new Tag[tagList.Values.Count];
            tagList.Values.CopyTo(tagsArray, 0);
            tag.GenerateID(tagsArray);
            tag.name = textBoxTag.Text;
            tagList.Add(tag.id, tag);
            textBoxTag.Text = "";

            ((Form1)(this.ParentForm)).UpdateComboBoxTags();
        }

        private void buttonDeleteTag_Click(object sender, EventArgs e)
        {
            if( listBoxTags.SelectedItem == null )
            {
                return;
            }

            Tag[] tagsArray = new Tag[tagList.Values.Count];
            tagList.Values.CopyTo(tagsArray, 0);
            foreach (Tag tag in tagsArray)
            {
                if (tag.name == (string)(listBoxTags.SelectedItem))
                {
                    tagList.Remove(tag.id);
                    break;
                }
            }
            listBoxTags.Items.Remove(listBoxTags.SelectedItem);
        }

        private void buttonAddXmlTag_Click(object sender, EventArgs e)
        {
            if (listBoxTags.SelectedItem == null || selectedXmlID.Count == 0)
            {
                return;
            }

            bool exists = false;
            for (int i = 0; i < listBoxXmlTags.Items.Count; i++)
                if (listBoxTags.SelectedItem == listBoxXmlTags.Items[i])
                    exists = true;
            if(!exists)
                listBoxXmlTags.Items.Add( listBoxTags.SelectedItem );

            if (GetSelectedTagIndex((string)(listBoxTags.SelectedItem)) != -1)
            {
                foreach (int i in selectedXmlID)
                {
                    XmlTagRelation relation = new XmlTagRelation();
                    relation.tagID = GetSelectedTagIndex((string)(listBoxTags.SelectedItem));
                    relation.xmlFileID = i;
                    relationsList.Add(relation);
                }

                
            }
        }

        private int GetSelectedTagIndex(string value)
        {
            Tag[] tagsArray = new Tag[tagList.Values.Count];
            tagList.Values.CopyTo(tagsArray, 0);
            foreach (Tag tag in tagsArray)
            {
                if (tag.name == value)
                {
                    return tag.id;
                }
            }
            return -1;
        }

        private void buttonDeleteXmlTag_Click(object sender, EventArgs e)
        {
            if (listBoxXmlTags.SelectedItem == null)
            {
                return;
            }

            int selectedIndex = GetSelectedTagIndex((string)(listBoxXmlTags.SelectedItem));

            List<XmlTagRelation> deleted_tags = new List<XmlTagRelation>();
            foreach(XmlTagRelation item in relationsList)
            {
                if (item.tagID == selectedIndex)
                {
                    foreach(int id_xml in selectedXmlID)
                        if(id_xml == item.xmlFileID)
                            //relationsList.Remove(item);
                            deleted_tags.Add(item);
                }
            }

            foreach(XmlTagRelation item in deleted_tags)
                relationsList.Remove(item);

            listBoxXmlTags.Items.Remove(listBoxXmlTags.SelectedItem);
        }

        private List<int> selectedXmlID;
    }
}
